<?php
declare(strict_types=1);

namespace Service\DeclareService\PayDeclareCompany;

use Service\BaseService;
use Service\Code\Normal;
use App\Model\BbcModel\Order;
use Service\VenderClient\PayCenterApi\AllinpayApiClient;
use App\Model\BbcModel\OrderReportLog;
use App\Model\BbcModel\PayReport;
use App\Model\BbcModel\OrderReportStatus;
use Service\MessageService\PayNotifyService;
use Service\DeclareService\PayDeclareService;
use App\Model\BbcModel\PushAgain;
use Service\PushService\PushYunCangApiService;

class Allinpay extends BaseService
{
    public function reportPay(array $body):array {
        $reportOrder=$body['reportOrder'];
        $payconfig=$body['payconfig'];
        $company=$body['company'];
        $cid=$body['cid'];
        if(empty($reportOrder) || empty($payconfig) || empty($company)){
            return Normal::returnCode(Normal::SUC, '参数缺失,丢弃该条数据');
        }
        //获取回调
        if(isset($body['domain'])&&!empty($body['domain'])){
            $domain=$body['domain'];
            $noticeUrl="{$domain}/paydeclare/notify/allinpay";
        }else{
            $callback_url = PayNotifyService::instance()->getChannelNotifyHost($cid);
            $noticeUrl="http://{$callback_url}/paydeclare/notify/allinpay";
        }
        $receive_info=json_decode($reportOrder[0]['receive_info'],true);
        $PAYMENT_AMOUNT= bcsub((string)$reportOrder[0]['price_order'], (string)$reportOrder[0]['points_money'],2);
        $report_data=[
            'MCHT_ORDER_NO'=>(string)$body['pay_sn'],
            'PAYMENT_DATETIME'=> date('YmdHis',$reportOrder[0]['pay_time']),
            'PAYMENT_ORDER_NO'=>(string)$body['pay_serial'],
            'PAYMENT_AMOUNT'=>(float)$PAYMENT_AMOUNT,
            'ESHOP_ENT_CODE'=>$company['eCompanyCode'],
            'ESHOP_ENT_NAME'=>$company['eCompanyName'],
            'PAYER_NAME'=>$receive_info['buyer']['truename'],
            'PAPER_NUMBER'=>$receive_info['buyer']['idcard'],
            'PAPER_PHONE'=>$receive_info['buyer']['mobile'],
        ];
        if($payconfig['customs_code']=='HG021'){
            $report_data['GOODS_FEE']= bcsub((string)$reportOrder[0]['price_order'], (string)$reportOrder[0]['tax_fee'],2);//商品货款金额
            $report_data['TAX_FEE']=(float)$reportOrder[0]['tax_fee'];//税款金额
            $report_data['FREIGHT_FEE']=0;//运费金额
        }
        if(count($reportOrder)==1){
            $body['pay_sn'] = $reportOrder[0]['sn'];
            OrderReportLog::instance()->add([
                'order_id'=>$reportOrder[0]['id'],
                'sn'=>$body['pay_sn'],
                'content'=>"发起支付单申报",
                'cd'=>time()
            ]);
            $client = (new AllinpayApiClient($payconfig));
            $result_call = $client->reportPay($report_data);
            PayReport::instance()->add([
                'order_id'=>$reportOrder[0]['id'],
                'sn'=>$body['pay_sn'],
                'content'=>json_encode($result_call,JSON_UNESCAPED_UNICODE)
            ]);
            if($result_call['code']==Normal::SUC){
                PayDeclareService::instance()->dealDeclareResult($body['pay_sn'], $reportOrder[0]['id'], '支付单申报成功');
                
                if($reportOrder[0]['need_order_report']==1){
                        //TODO订单申报
                    }
                
            }else{
                PayDeclareService::instance()->dealDeclareResult($body['pay_sn'], $reportOrder[0]['id'], '支付单申报失败:'.$result_call['msg'],false);
            }
        }else{
            //拆单申报 todo
            $extendOrder=[];
            $orderSNs= array_column($reportOrder, 'sn');
            $OrderReportStatusList=OrderReportStatus::instance()->lists([['sn','in',$orderSNs]]);
            $OrderReportStatusMapList= array_column($OrderReportStatusList, Null,'sn');
            $subOrderExtends=[
                'OrderReportStatusMapList'  =>$OrderReportStatusMapList,
                'pay_serial_extend'         =>''
            ];
            foreach ($reportOrder as $v){
                OrderReportLog::instance()->add(['order_id'=>$v['id'],'sn'=>$v['sn'],'content'=>"发起支付单申报",'cd'=>time()]);
                OrderReportLog::instance()->add(['order_id'=>$v['id'],'sn'=>$v['sn'],'content'=>"暂不支持拆单申报",'cd'=>time()]);
                PayDeclareService::instance()->dealDeclareResult($v['sn'], $v['id'], '暂不支持拆单申报',false,$subOrderExtends);
            }
        }
        return Normal::returnCode(Normal::SUC, '操作成功');
    }
    
    public function notify(array $body) {
        //todo  
        return Normal::returnCode(Normal::SUC, '操作成功');
    }
}
